-- plant
-- create by cheny3
-- 种植植物

local function findGarden()
    -- 找一下当前有没有花园
    local pos;
    for i = 1, GRID_SIZE do
        local grid = DungeonM.getGridByPos(i);
        if grid.type == GRID_TYPE_GARDEN
            and grid.state ~= GRID_STATE_DISABLE
            and grid:isOpened() then
            -- 已经打开未失效
            pos = i;
            break;
        end
    end
    return pos;
end

return {
    -- 判断
    canTrigger = function(args)
        local pos = findGarden();
        if not pos then
            -- 没有花园
            return false;
        end

        if ME.user.bine then
            -- 已经有魔藤/太阳花了
            return false;
        end

        local ok = false;
        if ItemM.getAmount(ME.user, 7257) > 0 then
            -- 有太阳花种子
            ok = true;
        end

        local bineLayer = args["bine_layer"];
        local layer = DungeonM.currentLayer();
        if bineLayer and layer >= bineLayer
            and ItemM.getAmount(ME.user, 7009) > 0 then
            -- 到达了限制层数
            ok = true;
        end

        -- 已经种过植物，但是现在没有植物，应该是种过太阳花了
        if TestFightM.getSpecialLog("plant")
            and ItemM.getAmount(ME.user, 7009) > 0 then
            ok = true;
        end

        return ok;
    end,

    -- 触发
    trigger = function(args)
        local pos = findGarden();
        if not pos then
            -- 沒有花园
            return false;
        end

        if ItemM.getAmount(ME.user, 7257) > 0 then
            -- 种太阳花
            return DungeonActionM.go("bine_grow", pos, 2);
        end

        -- 种魔藤
        return DungeonActionM.go("bine_grow", pos);
    end,
};
